package com.jiangxiaofei.citycloud.activity.recruit;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.EmptyUtils;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.JobBean;
import com.jiangxiaofei.citycloud.bean.WorkLabelBean;
import com.jiangxiaofei.citycloud.constants.Constants;
import com.jiangxiaofei.citycloud.constants.IntConstants;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.event.RefreshMessage;
import com.jiangxiaofei.citycloud.mvp.RecruitConstant;
import com.jiangxiaofei.citycloud.mvp.RecruitPersenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * Created by JiangXFei on 2017/9/8 0008.
 * 招工找人信息详情页
 */

public class RecruitFindWorkInfoActivity extends BaseActivity implements RecruitConstant.GetJobInfoView, View.OnClickListener {
    @InjectView(R.id.retruit_tv_work_type)
    TextView retruitTvWorkType;
    @InjectView(R.id.recruit_tv_describe)
    TextView recruitTvDescribe;
    @InjectView(R.id.recruit_tv_location)
    TextView recruitTvLocation;
    @InjectView(R.id.recruit_tv_bengin_time)
    TextView recruitTvBenginTime;
    @InjectView(R.id.recruit_tv_end_time)
    TextView recruitTvEndTime;
    @InjectView(R.id.recruit_tv_demand)
    TextView recruitTvDemand;
    @InjectView(R.id.recruit_tv_salary)
    TextView recruitTvSalary;
    @InjectView(R.id.recruit_tv_phone)
    TextView recruitTvPhone;
    @InjectView(R.id.recurit_img_phone)
    ImageView recuritImgPhone;
    @InjectView(R.id.recruit_tv_name)
    TextView recruitTvName;
    @InjectView(R.id.do_work_btn_pulish)
    Button doWorkBtnPulish;
    private RecruitConstant.GetJobInfoPersenter getJobInfoPersenter;
    private int workId;
    String[] perms = {android.Manifest.permission.CALL_PHONE};
    private String userType;

    @Override
    protected void initView(Bundle savedInstanceState) {
        mContext = this;
        mActivity = this;
        setContentView(R.layout.activity_recruit_find_work_info_layout);
        initTitle();
        ButterKnife.inject(this);
        getJobInfoPersenter = new RecruitPersenter(mContext, this);
        Intent intent = getIntent();
        String id = intent.getStringExtra(StringConstants.WORKID);
        userType = intent.getStringExtra(StringConstants.USERTYPE);
        workId = EmptyUtils.isNotEmpty(id) ? Integer.valueOf(id) : 0;
        getJobInfoPersenter.getJobInfo();
        Constants.bdLocation.getCity();
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refresh(RefreshMessage refreshMessage) {
        getJobInfoPersenter.getJobInfo();
    }

    @OnClick({R.id.do_work_btn_pulish, R.id.recurit_img_phone})
    public void OnClick(View v) {
        switch (v.getId()) {
            case R.id.do_work_btn_pulish:
                this.finish();
                break;
            case R.id.recurit_img_phone:
                if (EasyPermissions.hasPermissions(mContext, perms)) {
                    CallPhone();
                } else {
                    EasyPermissions.requestPermissions(this, "需要拨打电话权限",
                            IntConstants.CALLPHONECODE, perms);
                }
                break;
            default:
                break;
        }
    }

    private void CallPhone() {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + recruitTvPhone.getText().toString()));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    protected void setListener() {
        setCTitle("劳务信息详情");
        setLeftOneBg(R.drawable.ic_back);
        if (EmptyUtils.isNotEmpty(userType) && userType.equals(StringConstants.ISLIST)) {
            setRightThreeBg(R.drawable.ic_update);
        }
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        if (EmptyUtils.isNotEmpty(userType) && userType.equals(StringConstants.ISLIST)) {
            mRightThree.setOnClickListener(this);
        }
    }

    @Override
    public void setData(Object o) {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_rightthree:
                //更新
                Intent intent = new Intent(this, RecruitFindWorkPublishActivity.class);
                intent.putExtra(StringConstants.WORKID, workId);
                intent.putExtra(StringConstants.INTENTTYPE, StringConstants.UPDATE);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }

    @Override
    public void onSuccess(Object o) {
        List<JobBean> jobList = (List<JobBean>) o;
        JobBean jobBean;
        if (jobList.size() > 0) {
            jobBean = jobList.get(0);
            recruitTvDescribe.setText(jobBean.getDescription());
            String[] workTime = jobBean.getWorktime().split(",");
            recruitTvBenginTime.setText(workTime[0]);
            recruitTvEndTime.setText(workTime[1]);
            recruitTvName.setText(jobBean.getContacts());
            if (Integer.valueOf(jobBean.getPay()) > 0) {
                recruitTvSalary.setText(jobBean.getPay());
            } else {
                recruitTvSalary.setText("面议");
            }
            recruitTvDemand.setText(EmptyUtils.isNotEmpty(jobBean.getOther()) ? jobBean.getOther() : "面议");
            recruitTvPhone.setText(jobBean.getPhone());
            recruitTvLocation.setText(jobBean.getWorkplace());
            if (Constants.workLabelList != null && Constants.workLabelList.size() > 0) {
                String[] split = jobBean.getRole().split(",");
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < split.length; i++) {
                    Integer integer = Integer.valueOf(split[i]);
                    for (WorkLabelBean label : Constants.workLabelList) {
                        if (label.getId() == integer) {
                            if (sb.length() > 0) {
                                sb.append(",");
                            }
                            sb.append(label.getName());

                        }
                    }
                }
                retruitTvWorkType.setText(sb.toString());//工作标签
            } else {
                retruitTvWorkType.setText(jobBean.getRole());//工作标签
            }
        }
    }

    @Override
    public int getJobInfo() {
        return workId;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
